package top.zhangjianyong.tools.utils;

import lombok.extern.slf4j.Slf4j;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author zhangjianyong
 * @since 2025/3/18 下午3:48
 */
@Slf4j
public class SignatureUtil {


    public static String sha1(String data) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            byte[] digest = md.digest(data.getBytes(StandardCharsets.UTF_8));
            BigInteger no = new BigInteger(1, digest);
            StringBuilder hexString = new StringBuilder(no.toString(16));
            while (hexString.length() < 40) {
                hexString.insert(0, '0');
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            log.error(e.getMessage(), e);
            return null;
        }

    }


    public static String sha1(String... data) {
        List<String> list = Arrays.asList(data);
        Collections.sort(list);
        String dataStr = String.join("", list);
        return sha1(dataStr);
    }

    public static void main(String[] args) {
        String s = sha1("1742285174", "839657527", "920f04af954c45c0b6635e354f2a18be");
        System.out.println(s);
    }
}
